/*=============================================================================
                    AUTOMATED LOGIC CORPORATION
            Copyright (c) 1999 - 2010 All Rights Reserved
     This document contains confidential/proprietary information.
===============================================================================

   @(#)PropertiesFileReader

   Author(s) Matt
   Created: Oct 27, 2010, 10:35:42 PM
 
===============================================================================*/
package Utilities;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 <!=========================================================================>
 This should read all the properties to build up the buttons
 Date: Oct 27, 2010
 Time: 10:35:42 PM
 @author Matt
 <!==========================================================================> */
public class ButtonProperties
{


    List<TitledPanel> titledPanels = new ArrayList<TitledPanel>();

    public ButtonProperties() {

        //Create the XML Reader
        SAXBuilder saxBuilder = new SAXBuilder( false );

        //Create the XML Document
        Document document = null;
        try
        {
            //Load file into the XML document
            document = saxBuilder.build( new File( "e:\\programming\\RailsCodes.xml" ) );
        } catch( Exception e )
        {
            e.printStackTrace();
        }

        //Get the root element of the XML document
        Element root = document.getRootElement();

        //load the view buttons into memory
        initViewButtons( root );

    }

    private void initViewButtons( Element root ) {

        //Create the children under rails so we can get the views panel
        List<Element> children = root.getChildren( "rails" );

        //Create the XML element to keep the views node
        Element views = null;

        //find the view panel
        for( Element child : children )
        {

            //Get the attribute 'panel'. we are looking for the 'view' tab right now
            Attribute attribute = child.getAttribute( "panel" );

            //If it's the view panel then save it as the view panel
            if( attribute.getValue().equals( "view" ) )
                views = child;

        }

        //Get all the 'views' panels
        List<Element> panels = views.getChildren( "panel" );

        //Loop through all panels, add the panels to our list, then go through all their virtual buttons and
        //add them to our list too.
        for( Element panel : panels )
        {
            //Create a panel that has a title and set it to the panel's title
            TitledPanel titledPanel = new TitledPanel( panel.getAttributeValue( "title" ) );

            //Add the panel to our list of panels
            titledPanels.add( titledPanel );

            //Get all virtual buttons for this panel
            List<Element> virtuals = panel.getChildren( "virtual" );

            //Go through all buttons, and add them to our titled panel object.
            for( Element child : virtuals )
            {
                titledPanel.addButton( new VirtualButton( child.getAttributeValue( "displayName" ), child.getText() ) );
            }
        }

    }

    public List<TitledPanel> getTitledPanels() {
        return titledPanels;
    }
}